/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.araujogrosz.fractalcore;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelListener;
import javax.swing.JFrame;

/**
 *
 * @author renatogrosz@gmail.com
 */
public class JanelaThreadFractal extends JFrame implements ImageDrawner{

	private static final long serialVersionUID = 1L;
	private ThreadFractal threadFractal;
    private final int NUMBER_OF_THREADS = 256;
    private MouseWheelListener mouseWellListener;
    private MouseListener mouseListener;
    JanelaThreadFractal(int imageSize, ColorMatrix colorMatrix){
        this.finalizer = new LocalFinalizer();
        this.imageSize = imageSize;
        this.colorMatrix = colorMatrix; 
        this.setSize(imageSize + WindowParam.LEFT_BORDER + 5, imageSize + WindowParam.WINDOW_TITLE_HEIGHT + 5);
        
        start();
        
        threadFractal.calc();
    }
    
    private int imageSize;
    private ColorMatrix colorMatrix;
    
    private void start(){
        threadFractal = new ThreadFractal(NUMBER_OF_THREADS, imageSize, colorMatrix);  
        this.mouseWellListener = new DefaultMouseListener(threadFractal, threadFractal, this);
        this.mouseListener = new LocalMouseListener();
        super.addMouseWheelListener(mouseWellListener);
        super.addMouseListener(mouseListener);        
    }
    private void stop(){
        super.removeMouseListener(mouseListener);
        super.removeMouseWheelListener(mouseWellListener);
    }
    
        @Override
    public void paint(Graphics g) {
        super.paint(g);
        this.drawImage();
    }
    
     @Override
    public void drawImage() {
        int px =  WindowParam.LEFT_BORDER;
        int py = WindowParam.WINDOW_TITLE_HEIGHT;
        Image buf = threadFractal.getBuf();
        super.getGraphics().drawImage(buf, px, py, this);        
    }

    
     
    private class LocalMouseListener implements MouseListener{

        @Override
        public void mouseClicked(MouseEvent e) {
            JanelaThreadFractal.this.stop();
            JanelaThreadFractal.this.start();
            JanelaThreadFractal.this.threadFractal.calc();
            JanelaThreadFractal.this.drawImage();
        }

        @Override
        public void mousePressed(MouseEvent e) {}

        @Override
        public void mouseReleased(MouseEvent e) {}

        @Override
        public void mouseEntered(MouseEvent e) {}

        @Override
        public void mouseExited(MouseEvent e) {}        
    }

    
    private JanelaThreadFinalizer finalizer;
    
    @Override
    public void finalize() throws Throwable{
        try {
            finalizer.callOnFinalize();
        } finally {
            super.finalize();
        }
    }    
    public void setFinalizer(JanelaThreadFinalizer finalizer){
        if(finalizer == null) throw new NullPointerException("Finalizer nao pode ser null");
        this.finalizer = finalizer;        
    }
    
    
    private class LocalFinalizer implements JanelaThreadFinalizer{
        @Override public void callOnFinalize() {}        
    }               
}
